__author__ = 'tarek'
#script to upload keystats .

from twitterSentiment.models import Company, CompanyKeyStats
import pandas as pd
import ast
import sys

def run():
    allcompanykeystatscsv = 'data/keystats_results.csv'
    allcompanykeystats = pd.read_csv(allcompanykeystatscsv)

    for idx,row in allcompanykeystats.iterrows():
            company = Company.objects.get(symbol=row["Symbol"])
            try:
                keystats = ast.literal_eval(row["keystats"])
                companyKeystat = CompanyKeyStats(company_id=company.id,
                    marketcap = keystats[0],
                    marketcap_term = keystats[1],
                    enterprisevalue = keystats[2],
                    enterprisevalue_term = keystats[3],
                    trailingpe = keystats[4],
                    trailingpe_term = keystats[5],
                    forwardpe = keystats[6],
                    forwardpe_term = keystats[7],
                    pegratio = keystats[8],
                    pegratio_term = keystats[9],
                    pricesales = keystats[10],
                    pricesales_term = keystats[11],
                    pricebook = keystats[12],
                    pricebook_term = keystats[13],
                    enterprisevaluerevenue = keystats[14],
                    enterprisevaluerevenue_term = keystats[15],
                    enterprisevalueebitda = keystats[16],
                    enterprisevalueebitda_term = keystats[17],
                    fiscalyearends = keystats[18],
                    mostrecentquarter = keystats[19],
                    profitmargin = keystats[20],
                    profitmargin_term = keystats[21],
                    operatingmargin = keystats[22],
                    operatingmargin_term = keystats[23],
                    returnonassets = keystats[24],
                    returnonassets_term = keystats[25],
                    returnonequity = keystats[26],
                    returnonequity_term = keystats[27],
                    revenue = keystats[28],
                    revenue_term = keystats[29],
                    revenuepershare = keystats[30],
                    revenuepershare_term = keystats[31],
                    quarterlyrevenuegrowth = keystats[32],
                    quarterlyrevenuegrowth_term = keystats[33],
                    grossprofit = keystats[34],
                    grossprofit_term = keystats[35],
                    ebitda = keystats[36],
                    ebitda_term = keystats[37],
                    netincomeavltocommon = keystats[38],
                    netincomeavltocommon_term = keystats[39],
                    dilutedeps = keystats[40],
                    dilutedeps_term = keystats[41],
                    quarterlyearningsgrowth = keystats[42],
                    quarterlyearningsgrowth_term = keystats[43],
                    totalcash = keystats[44],
                    totalcash_term = keystats[45],
                    totalcashpershare = keystats[46],
                    totalcashpershare_term = keystats[47],
                    totaldebt = keystats[48],
                    totaldebt_term = keystats[49],
                    totaldebtequity = keystats[50],
                    totaldebtequity_term = keystats[51],
                    currentratio = keystats[52],
                    currentratio_term = keystats[53],
                    bookvaluepershare = keystats[54],
                    bookvaluepershare_term = keystats[55],
                    operatingcashflow = keystats[56],
                    operatingcashflow_term = keystats[57],
                    leveredfreecashflow = keystats[58],
                    leveredfreecashflow_term = keystats[59],
                    beta = keystats[60],
                    p52weekchange = keystats[61],
                    sp500p52weekchange = keystats[62],
                    p52weekhigh = keystats[63],
                    p52weekhigh_term = keystats[64],
                    p52weeklow = keystats[65],
                    p52weeklow_term = keystats[66],
                    p50daymovingaverage = keystats[67],
                    p200daymovingaverage = keystats[68],
                    averagevolume = keystats[69],
                    averagevolume_term = keystats[70],
                    sharesoutstanding = keystats[71],
                    sfloat = keystats[72],
                    percenthldbyinsiders = keystats[73],
                    percenthldbyinstitutions = keystats[74],
                    sharesshort = keystats[75],
                    shareshort_term = keystats[76],
                    shortratio = keystats[77],
                    shortratio_term = keystats[78],
                    shortpercentfloat = keystats[79],
                    shortpercentfloat_term = keystats[80],
                    forwardannualdividentrate = keystats[81],
                    forwardannualdividentyield = keystats[82],
                    trailingannualdividentrate = keystats[83],
                    p_5yearaveragedivident = keystats[84],
                    payoutratio = keystats[85],
                    dividentrate = keystats[86],
                    ex_dividentrate = keystats[87],
                    lastsplitfactor = keystats[88],
                    lastsplitfactor_term = keystats[89],
                    lastsplitdate = keystats[90],)
                companyKeystat.save()
            except:
                print ("error on row: ", idx, "company",company.name, "message:", sys.exc_info())